//
//  LatentCustomerView.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/22.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "LatentCustomerView.h"
#import "StringTool.h"
#import "UIImageView+WebCache.h"
#import "NSString+sizeWithFont.h"
#import "config.h"

#define NameFont [UIFont systemFontOfSize:13]


@interface LatentCustomerView()

@property (weak, nonatomic) IBOutlet UIImageView *headImg;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *mobileLab;

@property (nonatomic,strong) NSLayoutConstraint* conLay;
@property (weak, nonatomic) IBOutlet UIView *rightView;

@end
@implementation LatentCustomerView

+(instancetype)createViewWithModel:(CustomerModel *)model;
{
    LatentCustomerView *view =  [[[NSBundle mainBundle]loadNibNamed:@"LatentCustomerView" owner:nil options:nil]lastObject];
    [view addTiplabWithModel:model];
    
    for (NSLayoutConstraint *conLayout in view.constraints) {
        
        if (conLayout.firstItem==view.headImg&&conLayout.firstAttribute ==NSLayoutAttributeLeading) {
            view.conLay = conLayout;
            break;
        }
    }
    
    return view;
}

-(void)setModel:(CustomerModel *)model
{
    [self.headImg  sd_setImageWithURL:[NSURL URLWithString:model.consumerAvatar] placeholderImage:[UIImage imageNamed:@"user_portiart"]];
    if (![StringTool isBlankString:model.consumerName]) {
        self.nameLab.text = model.consumerName;
    }
    if (![StringTool isBlankString:model.consumerMobile]) {
        self.mobileLab.text = model.consumerMobile;
    }
    

   }

-(void)addTiplabWithModel:(CustomerModel *)model
{

    NSDictionary *attrs =@{NSFontAttributeName:NameFont};
    self.tipLab = [[UILabel alloc]init];
    self.tipLab.textColor = [UIColor whiteColor];
    self.tipLab.font = [UIFont systemFontOfSize:13];

    CGFloat tipLabH = 20;
    CGFloat tipLabX = 131;
    CGFloat tipLabY = 10;
    
    CGSize nameMaxSize = CGSizeMake(MAXFLOAT,tipLabH);
    if (model.dateNum >= 0 && model.dateNum <= 7)
    {
        self.tipLab.text = [NSString stringWithFormat:@"  %zd天内到期",model.dateNum];
        
        [self.tipLab setBackgroundColor:UIColorFromRGB(0xdb2a2b)];
    }else if (model.dateNum <= -3)
    {
        self.tipLab.text = [NSString stringWithFormat:@"  已过期"];
        [self.tipLab setBackgroundColor:[UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1]];
    }else if (model.dateNum<= 0 && model.dateNum > -3)
    {
        self.tipLab.text = [NSString stringWithFormat:@"  还剩%zd天可重新锁定",model.dateNum + 3];
        [self.tipLab setBackgroundColor:UIColorFromRGB(0x58bf66)];
    }
    CGFloat tipLabW =  [self.tipLab.text  boundingRectWithSize:nameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;
    self.tipLab.frame = CGRectMake(tipLabX, tipLabY, tipLabW+ 5*2, tipLabH);
    [self addSubview:self.tipLab];

}
-(void)setIsEditMode:(Boolean)isEditMode{
    
    [self.rightView setHidden:isEditMode];
    
    self.conLay.constant = isEditMode?40:10;
    
    [self layoutIfNeeded];
}


@end
